package com.atguigu.gmall.controller;

import com.atguigu.gmall.bean.TrafficUvCt;
import com.atguigu.gmall.service.TrafficStatsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Felix
 * @date 2024/11/12
 * 流量域统计controller
 */
@RestController
public class TrafficStatsController {

    @Autowired
    private TrafficStatsService trafficStatsService;

    @RequestMapping("/ch")
    public String getChUvCt(@RequestParam(value = "date", defaultValue = "0") Integer date,
                            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        List<TrafficUvCt> trafficUvCtList = trafficStatsService.getChUvCt(date, limit);
        List chList = new ArrayList();
        List uvCtList = new ArrayList();

        for (TrafficUvCt tc : trafficUvCtList) {
            chList.add(tc.getCh());
            uvCtList.add(tc.getUvCt());
        }

        String json = "{\"status\": 0,\"data\": {\n" +
                "    \"categories\": [\"" + StringUtils.join(chList, "\",\"") + "\"],\n" +
                "    \"series\": [{\"name\": \"渠道\",\"data\": [" + StringUtils.join(uvCtList, ",") + "]}]}}";
        return json;
    }
}
